// pages/my/logistics/logistics.js
var app = getApp()
Page({
  data: {
    logisticsType: '',
    logisticsList: [
    // {
    //   info: "货已到店，本次配送完成，感谢您此次购买，祝您生活愉快！",
    //   time:"2018-07-28   12:00",
    //   isNew:true,
    //    isLast: false
    // },{
    //     info: "新大洲货运已出发，请内心等待.联系电话18327345678！",
    //     time: "2018-07-28   12:00",
    //     isNew: false,
    //     isLast: false
    // },{
    //     info: "收到",
    //     time: "2018-07-28   12:00",
    //     isNew: false,
    //     isLast:true
    // }
    ],
    company:'',
    expressNo:''
  },
  onLoad: function(options) {

    wx.setNavigationBarTitle({
      title: '物流信息',
    })
    console.log(options)
    if (options.logisticType=='"第三方物流"') {
      if (options.orderId) {
        this.getLogistic(options.orderId)
      }else{
        this.getLogisticInfo(options.returnsId)
      }
      this.setData({
        logisticsType:1
      })
    }else if (options.logisticType=='"卖家配送"'){
      this.setData({
        logisticsType:0
      })
      this.getOtherLogisticInfo(options.orderId)
    }else{
      this.setData({
        logisticsType:2
      })
      this.getSelfLogisticInfo(options.orderId)
    }
    
  },
  getLogisticInfo(logistics_company,expressNo){
    let data = {
      logistics_company,
      expressNo
    }
    let logisticsList = []
    app.request('wx_app/after/sale/logistics/info',data,'GET').then(res=>{
      console.log(res)
      let list = res.data.detail
      list.forEach((item,index)=>{
        if (index==0) {
          logisticsList.push({
            info:item.logisticsDesc,
            time:item.logisticsTime,
            isNew:true
          })
        }else if (index==list.length-1) {
          logisticsList.push({
            info:item.logisticsDesc,
            time:item.logisticsTime,
            isLast:true
          })
        }else{
          logisticsList.push({
            info:item.logisticsDesc,
            time:item.logisticsTime
          })
        }
      })
      this.setData({
        logisticsList
      })
    })
  },
  //获取物流信息（第三方物流）
  getLogistic(orderId){
    let data = {
      orderId
    }
    console.log(data)
    app.request('wx_app/logistics/inquiryLogistics',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.Success) {
          console.log(res.data)
          let logisticsList = []
          res.data.Traces.forEach((item,index)=>{
            if (index==0) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isLast:true
              })
            }else if (index==res.data.Traces.length-1) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isNew:true
              })
            }else{
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime
              })
            }
          })
          this.setData({
            company:res.data.ShipperName,
            expressNo:res.data.LogisticCode,
            logisticsList
          })
        }else{
          app.errorMsg('没有物流信息')
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //退货物流信息
  getLogisticInfo(returnsId){
    let data = {
      returnsId
    }
    console.log(data)
    app.request('wx_app/logistics/getReturnOrderLogistics',data,'GET').then(res=>{
      console.log(res)

      if (res.code==1) {
        if (res.data.Success) {
          console.log(res.data)
          let logisticsList = []
          res.data.Traces.forEach((item,index)=>{
            if (index==0) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isLast:true
              })
            }else if (index==res.data.Traces.length-1) {
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime,
                isNew:true
              })
            }else{
              logisticsList.unshift({
                info:item.AcceptStation,
                time:item.AcceptTime
              })
            }
          })
          this.setData({
            company:res.data.ShipperName,
            expressNo:res.data.LogisticCode,
            logisticsList
          })
        }else{
          app.errorMsg('没有物流信息')
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //获取商家配送和自提的时间信息
  getOtherLogisticInfo(orderId){
    let that = this
    let data = {
      orderId
    }
    app.request('wx_app/agent_order/detailByState',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.orderState=="已完成") {
          that.setData({
            ['logisticsList[0].info']:'货已到店，本次配送完成，感谢您此次购买，祝您生活愉快！',
            ['logisticsList[0].time']:res.data.logisticsTime,
            ['logisticsList[0].isNew']:true,
            ['logisticsList[1].info']:'货运已出发，请内心等待。联系电话'+res.data.logisticsPhone,
            ['logisticsList[1].time']:res.data.deliveryTime,
            ['logisticsList[1].isLast']:true
          })
        }else{
          that.setData({
            ['logisticsList[0].info']:'货运已出发，请内心等待。联系电话'+res.data.logisticsPhone,
            ['logisticsList[0].time']:res.data.deliveryTime,
            ['logisticsList[0].isNew']:true
          })
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  getSelfLogisticInfo(orderId){
    let that = this
    let data = {
      orderId
    }
    app.request('wx_app/agent_order/detailByState',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.orderState=="已完成") {
          that.setData({
            ['logisticsList[0].info']:'您已到店自提，感谢您此次购买，祝您生活愉快!',
            ['logisticsList[0].time']:res.data.logisticsTime,
            ['logisticsList[0].isNew']:true,
            ['logisticsList[1].info']:'',
            ['logisticsList[1].time']:res.data.deliveryTime,
            ['logisticsList[1].isLast']:true
          })
        }else{
          that.setData({
            ['logisticsList[0].info']:'',
            ['logisticsList[0].time']:res.data.logisticsTime,
            ['logisticsList[0].isNew']:true
          })
        }
        
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})